package org.crazydog.converter.feet;

import java.util.Scanner;

import org.crazydog.converter.constants.Feet;

/**
 * Performs feet to meter and meter to feet conversions
 * 
 * @author James, Jordan, George
 * 
 */
public class Calculator {

	/**
	 * Default constructor
	 * 
	 */
	private Calculator() {
		super();
	}

	public final static int feet = 0;

	public final static int meter = 1;

	public final static double error = -100;

	public final static double errorNoString = -200;

	public final static double errorNegative = -300;

	private final static String[] errors = new String[] { "Unknown Error",
			"No number entered", "Please don't use negative numbers" };

	private static String[] metrics = new String[] { "Feet", "Meter" };

	/**
	 * Converts feet to meters
	 * 
	 * @param Double 
	 * @return Double
	 */
	public static double ConvertFeetToMeters(double feet) {
		return feet * Feet.FeetToMeters;
	}

	/**
	 * Converts meters to feet
	 * 
	 * @param Double
	 * @return Double
	 */
	public static double ConvertMetersToFeet(double meters) {
		return meters * Feet.MetersToFeet;
	}

	/**
	 * Converts the given lenghts.
	 * 
	 * @param Int
	 * @param Int 
	 * @param String
	 * @return Double
	 */
	public static double Convert(int from, int to, String number) {
		double result = 0;
		// number = number.replaceAll(" ", "");
		Scanner scan = new Scanner(number);
		if (scan.hasNextDouble()) { // Checks if string is double.
			double n = Double.parseDouble(number);
			if (n < 0) {
				result = errorNegative;
			} else if (from == meter && to == feet) {
				result = Calculator.ConvertMetersToFeet(n);
			} else if (from == feet && to == meter) {
				result = Calculator.ConvertFeetToMeters(n);
			} else if (from == to) {
				result = Double.parseDouble(number);
			}
		} else if (number.equals("")) { // Checks if nothing has been entered.
			result = errorNoString;
		}

		else { // Else string considered invalid.
			result = error;
		}
		//
		return result;
	}

	/**
	 * Check is output is error.
	 * 
	 * @param Double
	 * @return Boolean
	 */
	public static boolean isError(double errorCode) {
		return errorCode <= -100;
	}

	/**
	 * Returns Metrics.
	 * 
	 * @return Object
	 */
	public static Object[] getMetrics() {
		return metrics;
	}

	/**
	 * Return a String discribing the error.
	 * 
	 * @param Double
	 * @return String
	 */
	public static String getError(double errorCode) {
		return errors[(int) (errorCode / -100) - 1];
	}
}
